package com.hjb.service.impl.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hjb.service.app.BaseAreaService;
import com.hjb.service.config.constant.Key;
import com.hjb.service.domain.BaseArea.BaseArea;
import com.hjb.service.mapper.BaseAreaMapper;

import tk.mybatis.mapper.entity.Example;

@Service
public class BaseAreaServiceImpl implements BaseAreaService {
	@Autowired
	private BaseAreaMapper baseAreaMapper;
	
	@Override
	public List<BaseArea> getTopParents() {
		Example example = new Example(BaseArea.class);
		example.createCriteria().andEqualTo("status", 1).andIsNull("parentId");
		return baseAreaMapper.selectByExample(example);
	}
	@Override
	public List<BaseArea> getBaseAreasByParentId(String parentId) {
		Example example = new Example(BaseArea.class);
		example.createCriteria().andEqualTo("status", 1).andEqualTo("parentId",parentId);
		return baseAreaMapper.selectByExample(example);
	}
	/* 
	 * 根据区域NO获取省市信息
	 */
	@Override
	public Map<String, Object> getBaseAreasByRegionNo(String regionNo) {
		Map<String, Object> area = new HashMap<String,Object>();
		
		Map<String, String> REGION = new HashMap<String,String>();
		Map<String, String> CITY = new HashMap<String,String>();
		Map<String, String> PROVINCE = new HashMap<String,String>();
		//查询区域
		Example example = new Example(BaseArea.class);
		example.createCriteria().andEqualTo("id",regionNo);
		List<BaseArea> array = baseAreaMapper.selectByExample(example);
		BaseArea region=array.get(0);
		REGION.put(Key.NAME,region.getName());
		REGION.put(Key.ID,region.getId());
		
		//查询城市
		Example example1 = new Example(BaseArea.class);
		example1.createCriteria().andEqualTo("id",region.getParentId());
		List<BaseArea> array1 = baseAreaMapper.selectByExample(example1);
		BaseArea city=array1.get(0);
		CITY.put(Key.NAME,city.getName());
		CITY.put(Key.ID,city.getId());
		
		//查询省份
		Example example2 = new Example(BaseArea.class);
		example2.createCriteria().andEqualTo("id",city.getParentId());
		List<BaseArea> array2 = baseAreaMapper.selectByExample(example2);
		BaseArea province=array2.get(0);
		PROVINCE.put(Key.NAME,province.getName());
		PROVINCE.put(Key.ID,province.getId());
		
		area.put(Key.PROVINCE, PROVINCE);
		area.put(Key.CITY, CITY);
		area.put(Key.REGION, REGION);
		return area;
	}

}
